package com.fhao.controller;

import java.util.LinkedHashMap;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.fhao.feign.MemberServiceFeign;
import com.fuhao.base.ResponseBase;
import com.fuhao.constants.Constants;
import com.fuhao.utils.CookieUtil;

@Controller
public class IndexController {
	
	@Autowired
	private MemberServiceFeign MemberServiceFeign;
	
	//主页
	@RequestMapping(value="/")
	public String index(HttpServletRequest request) {
		//从cookie获取token
		String token = CookieUtil.getUid(request, "cookie_member_token");
		//根据token查询用户信息
		if(!StringUtils.isEmpty(token)) {
			ResponseBase responseBase = MemberServiceFeign.findByTokenUser(token);
			if(responseBase.getCode().equals(Constants.HTTP_RES_CODE_200)) {
				LinkedHashMap userDate = (LinkedHashMap) responseBase.getData();
				String username = (String) userDate.get("username");
				request.setAttribute("username", username);
			}
		}
		return "index";
	}
}
